﻿//++
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
// Module Name:
//
//    EnhancedRemindersRibbonTest.cs
//
// Abstract:
//
//    Unit tests for EnhancedRemindersRibbon class
//
//--

using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using Microsoft.Office.Interop.Outlook;
using Microsoft.Office.Tools.Ribbon;
using Microsoft.OfficeCommunicationsServer.Applications.Common;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TestHelpers = Microsoft.EnhancedReminders.UnitTests.Helpers;

namespace Microsoft.EnhancedReminders.OutlookAddIn.UnitTests
{
    /// <summary>
    ///This is a test class for EnhancedRemindersRibbonTest and is intended
    ///to contain all EnhancedRemindersRibbonTest Unit Tests
    ///</summary>
    [TestClass()]
    [SuppressMessage("Microsoft.Design", "CA1001:TypesThatOwnDisposableFieldsShouldBeDisposable",
        Justification = "AutoResetEvents are enclosed within using block")]
    public sealed class EnhancedRemindersRibbonTest
    {
        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for TimeCombo_TextChanged
        ///</summary>
        [TestMethod()]
        public void TimeComboTextChangedTest()
        {
            MockExchangeSession exchangeSession = new MockExchangeSession(OlExchangeConnectionMode.olOnline);
            Debug.Assert(exchangeSession != null, "exchangeSession != null");

            EnhancedRemindersRibbon_Accessor ribbon = new EnhancedRemindersRibbon_Accessor();
            Debug.Assert(ribbon != null, "ribbon != null");
            ribbon._exchangeSession = exchangeSession;

            OutlookReminderInspector_Accessor inspector = new OutlookReminderInspector_Accessor(new MockInspector(new MockAppointmentItem(OlResponseStatus.olResponseAccepted)));
            TestHelpers.WaitOne(inspector._backgroundWorkerCompletedEvent, "_backgroundWorkerCompletedEvent");

            ribbon._reminderInspector = inspector.Target as OutlookReminderInspector;

            ribbon.TimeCombo.Text = "60 minutes";
            ribbon.TimeCombo_TextChanged(ribbon.TimeCombo, null);
            string actual = ribbon.TimeCombo.Text;
            Assert.AreEqual("1 hour", actual, "Default time should be changed to 60 minutes");
        }

        /// <summary>
        ///A test for TimeCombo_TextChanged
        ///</summary>
        [TestMethod()]
        public void JoinNowTimeComboTextChangedTest()
        {
            MockExchangeSession exchangeSession = new MockExchangeSession(OlExchangeConnectionMode.olOnline);
            Debug.Assert(exchangeSession != null, "exchangeSession != null");

            EnhancedRemindersRibbon_Accessor ribbon = new EnhancedRemindersRibbon_Accessor();
            Debug.Assert(ribbon != null, "ribbon != null");
            ribbon._exchangeSession = exchangeSession;

            OutlookReminderInspector_Accessor inspector = new OutlookReminderInspector_Accessor(new MockInspector(new MockAppointmentItem(OlResponseStatus.olResponseAccepted, Constants.LiveMeetingMessageClass)));
            TestHelpers.WaitOne(inspector._backgroundWorkerCompletedEvent, "_backgroundWorkerCompletedEvent");

            ribbon._reminderInspector = inspector.Target as OutlookReminderInspector;

            ribbon.JoinNowTimeCombo.Text = "60 minutes";
            ribbon.JoinNowTimeCombo_TextChanged(ribbon.Target, null);
            string actual = ribbon.JoinNowTimeCombo.Text;
            Assert.AreEqual("1 hour", actual, "Default time should be changed to 60 minutes");
        }

        /// <summary>
        ///A test for InitializeControls
        ///</summary>
        [TestMethod()]
        public void InitializeControlsTest()
        {
            EnhancedRemindersRibbon_Accessor ribbon = new EnhancedRemindersRibbon_Accessor();
            Debug.Assert(ribbon != null, "ribbon != null");

            OutlookReminderInspector_Accessor reminderInspector = 
                new OutlookReminderInspector_Accessor(new MockInspector(new MockAppointmentItem(OlResponseStatus.olResponseAccepted, string.Empty)));
            Debug.Assert(reminderInspector != null, "reminderInspector != null");

            // Default ReminderTime should be the first item in the list - None
            string expected = ReminderOutlookUtils_Accessor.DefaultReminderTimes[0];
            string actual;


            ribbon._reminderInspector = reminderInspector.Target as OutlookReminderInspector;
            ribbon.InitializeControls();
            actual = ribbon.TimeCombo.Text;
            Assert.AreEqual(expected, actual, "Default time should be None");
            Assert.AreEqual(expected, ribbon.JoinNowTimeCombo.Text, "Default join now time should be None");
        }

        /// <summary>
        ///A test for TimeComboImageMso
        ///</summary>
        [TestMethod()]
        public void TimeComboGetIconTest()
        {
            EnhancedRemindersRibbon_Accessor ribbon = new EnhancedRemindersRibbon_Accessor();
            Debug.Assert(ribbon != null, "ribbon != null");

            OutlookReminderInspector_Accessor inspector = new OutlookReminderInspector_Accessor(new MockInspector(new MockAppointmentItem(OlResponseStatus.olResponseNone)));
            Debug.Assert(inspector != null, "inspector != null");
            ribbon._reminderInspector = inspector.Target as OutlookReminderInspector;
            MockExchangeSession exchangeSession = new MockExchangeSession(OlExchangeConnectionMode.olOnline);
            ribbon._exchangeSession = exchangeSession;

            TestHelpers.WaitOne(inspector._backgroundWorkerCompletedEvent, "_backgroundWorkerCompletedEvent");

            // Default ReminderTime should be the first item in the list - None
            ribbon.RefreshControls();
            string actual = ribbon.TimeComboImageMso;
            string expected = Properties.Resources.ConnectedImageMso;
            Assert.AreEqual(expected, actual, "Incorrect icon name");

            // Test disconnected case
            exchangeSession = new MockExchangeSession(OlExchangeConnectionMode.olDisconnected);
            ribbon._exchangeSession = exchangeSession;
            ribbon.RefreshControls();

            // Default ReminderTime should be the first item in the list - None
            actual = ribbon.TimeComboImageMso;
            expected = Properties.Resources.DisconnectedImageMso;
            Assert.AreEqual(expected, actual, "Incorrect icon name");
        }


        /// <summary>
        ///A test for ChannelCombo's enabled status. Channel combo should be disabled
        ///by default as the time selected is "None".
        ///</summary>
        [TestMethod()]
        public void ChannelComboEnabledDefaultTest()
        {
            EnhancedRemindersRibbon_Accessor ribbon = new EnhancedRemindersRibbon_Accessor();
            Debug.Assert(ribbon != null, "ribbon != null");
            OutlookReminderInspector_Accessor inspector = new OutlookReminderInspector_Accessor(new MockInspector(new MockAppointmentItem(OlResponseStatus.olResponseNone)));
            Debug.Assert(inspector != null, "inspector != null");
            ribbon._reminderInspector = inspector.Target as OutlookReminderInspector;

            bool actual;
            actual = ribbon.NotificationChannels.Enabled;
            Assert.IsFalse(actual, "Default ReminderItem should have the ReminderTime = -1");
        }

        /// <summary>
        ///A test for ChannelCombo's enability. Verify that when the
        ///time is changed to a valid entry, the channel combo is
        ///enabled
        ///</summary>
        [TestMethod()]
        public void ChannelComboEnabledTest()
        {
            EnhancedRemindersRibbon_Accessor ribbon = new EnhancedRemindersRibbon_Accessor();
            Debug.Assert(ribbon != null, "ribbon != null");
            MockExchangeSession exchangeSession = new MockExchangeSession(OlExchangeConnectionMode.olOnline);
            ribbon._exchangeSession = exchangeSession;

            OutlookReminderInspector_Accessor outlookInspector = new OutlookReminderInspector_Accessor(new MockInspector(new MockAppointmentItem(OlResponseStatus.olResponseAccepted)));
            Debug.Assert(outlookInspector != null, "outlookInspector != null");

            ribbon._reminderInspector = outlookInspector.Target as OutlookReminderInspector;
            TestHelpers.WaitOne(outlookInspector._backgroundWorkerCompletedEvent, "_backgroundWorkerCompletedEvent");
            ribbon.RefreshControls();

            bool actual;
            actual = ribbon.NotificationChannels.Enabled;
            Assert.IsFalse(actual, "Channel dropdown is disabled until time is set");

            outlookInspector.LeadTime = 60;
            ribbon.RefreshControls();

            actual = ribbon.NotificationChannels.Enabled;
            Assert.IsTrue(actual, "Channel dropdown should be enabled when time is not None");

            Assert.IsFalse(ribbon.JoinNowChannels.Enabled, "Join Now dropdown should not be enabled when notification time has changed.");
        }

        /// <summary>
        ///A test for ChannelCombo's enability. Verify that when the
        ///time is changed to a valid entry, the channel combo is
        ///enabled
        ///</summary>
        [TestMethod()]
        public void JoinNowChannelComboEnabledTest()
        {
            EnhancedRemindersRibbon_Accessor ribbon = new EnhancedRemindersRibbon_Accessor();
            Debug.Assert(ribbon != null, "ribbon != null");
            MockExchangeSession exchangeSession = new MockExchangeSession(OlExchangeConnectionMode.olOnline);
            ribbon._exchangeSession = exchangeSession;

            OutlookReminderInspector_Accessor outlookInspector = new OutlookReminderInspector_Accessor(new MockInspector(new MockAppointmentItem(OlResponseStatus.olResponseAccepted, Constants.LiveMeetingMessageClass)));
            Debug.Assert(outlookInspector != null, "outlookInspector != null");

            ribbon._reminderInspector = outlookInspector.Target as OutlookReminderInspector;
            TestHelpers.WaitOne(outlookInspector._backgroundWorkerCompletedEvent, "_backgroundWorkerCompletedEvent");
            ribbon.RefreshControls();

            bool actual;
            actual = ribbon.JoinNowChannels.Enabled;
            Assert.IsFalse(actual, "Channel dropdown is disabled until time is set");

            outlookInspector.JoinNowLeadTime = 60;
            ribbon.RefreshControls();

            actual = ribbon.JoinNowChannels.Enabled;
            Assert.IsTrue(actual, "Channel dropdown should be enabled when time is not None");

            Assert.IsFalse(ribbon.NotificationChannels.Enabled, "Notification dropdown should not be enabled when notification time has changed.");
        }

        /// <summary>
        /// Test for the join now controls being visible when the message is a Live Meeting.
        /// </summary>
        [TestMethod()]
        public void TestJoinNowVisible()
        {
            EnhancedRemindersRibbon_Accessor ribbon = new EnhancedRemindersRibbon_Accessor();
            Debug.Assert(ribbon != null, "ribbon != null");
            MockExchangeSession exchangeSession = new MockExchangeSession(OlExchangeConnectionMode.olOnline);
            ribbon._exchangeSession = exchangeSession;

            OutlookReminderInspector_Accessor outlookInspector = new OutlookReminderInspector_Accessor(new MockInspector(new MockAppointmentItem(OlResponseStatus.olResponseAccepted, Constants.LiveMeetingMessageClass)));
            Debug.Assert(outlookInspector != null, "outlookInspector != null");

            ribbon._reminderInspector = outlookInspector.Target as OutlookReminderInspector;
            TestHelpers.WaitOne(outlookInspector._backgroundWorkerCompletedEvent, "_backgroundWorkerCompletedEvent");
            ribbon.RefreshControls();

            Assert.IsTrue(ribbon.JoinNowChannels.Visible, "Join Now channels control should be visible.");
            Assert.IsTrue(ribbon.JoinNowTimeCombo.Visible, "Join Now time combo control should be visible.");
            Assert.IsTrue(ribbon.AutoJoinLabel.Visible, "Join Now label should be visible.");
        }
    }
}
